﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Qplus.Commands;
using Qplus.beans;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Microsoft.Phone.Controls;

namespace Qplus.UI
{
    public class UIData
    {
        QUser user = QplusT.Instance.getUser();
        //UserInfo friendInfo = new UserInfo();
        Room room = new Room();
        Session session = new Session();
        AppInfo appinfo = new AppInfo();

        public event EventHandler<UIEvent> getDataEvent;

        public HttpCommand httpCMD { get; set; }
        private static UIData instance;

        public UIData()
        {
            userList = new userList();
        }
        public static UIData Instance { get { if (instance == null) instance = new UIData(); return instance; } }

        public l2cInfo L2C { get; set; }
        public u2cRESPmsg U2C { get; set; }
        //public userInfo userInfo { get; set; }
        public userList userList { get; set; }
        public roomList roomList { get; set; }

#region Room Command
        public bool CreateRoom(String roomName, int headType, String headValue)
        {
            CreateRoomCommand command = new CreateRoomCommand(user.userID,user.key);
            command.set( roomName, headType, headValue);
            command.execute(user.url);
            return true;
        }

        public bool GetRoomList(int pageNum)
        {
            GetRoomListCommand command = new GetRoomListCommand(user.userID.ToString(), System.Text.Encoding.UTF8.GetString(user.key, 0, user.key.Length));
            httpCMD = command;
            command.set(pageNum);
            command.execute(user.url);
            return true;
        }

        public bool ModifyRoom(long roomID, String roomName, int headType, String headValue)
        {
            ModifyRoomCommand command = new ModifyRoomCommand(L2C.UserID.ToString(), System.Text.Encoding.UTF8.GetString(L2C.Key, 0, L2C.Key.Length), room);
            command.set(roomID, roomName, headType, headValue);
            command.execute(L2C.UpgradeURL);
            return true;
        }
#endregion
#region Info Command

        public bool RegMachine()
        {
            RegMachineCommand command = new RegMachineCommand(L2C.UserID.ToString(), System.Text.Encoding.UTF8.GetString(L2C.Key, 0, L2C.Key.Length));
            command.set(L2C.UserID, "phoneType", appinfo.appID, "platform", "imsi", "imei");
            command.execute(L2C.UpgradeURL);
            return true;
        }
        public bool GetUser()
        {
            GetUserCommand command = new GetUserCommand(user.userID.ToString(), System.Text.Encoding.UTF8.GetString(user.key, 0, user.key.Length));
            command.execute(user.url);
            return true;
        }
        public void ModifyUser(UserInfo userinfo)
        {
            ModifyUserCommand command = new ModifyUserCommand(user.userID.ToString(), System.Text.Encoding.UTF8.GetString(user.key, 0, user.key.Length), QplusT.getInstance().getUser());
            command.set(userinfo.name, userinfo.mail, "", userinfo.birthday, userinfo.sex, userinfo.canStrangerChat, userinfo.headType, userinfo.headValue);//, null);
            command.execute(user.url);
        }
        public bool ForgotPwd(string mail)
        {
            ForgotPwdCommand command = new ForgotPwdCommand(L2C.UserID.ToString(), System.Text.Encoding.UTF8.GetString(L2C.Key, 0, L2C.Key.Length));
            command.set( Convert.ToInt64( appinfo.appID), mail);
            command.execute(L2C.UpgradeURL);
            return true;
        }

        public bool GetFile(string resid)
        {
            GetFileCommand command = new GetFileCommand(L2C.UserID.ToString(), System.Text.Encoding.UTF8.GetString(L2C.Key, 0, L2C.Key.Length), session,0, "fff");
            command.set(resid);
            command.execute(L2C.UpgradeURL);
            return true;
        }

        public bool GetRes(long resid)
        {
            GetResCommand command = new GetResCommand(L2C.UserID.ToString(), System.Text.Encoding.UTF8.GetString(L2C.Key, 0, L2C.Key.Length));
            command.set(resid);
            command.execute(L2C.UpgradeURL);
            return true;
        }

        public bool GetOtherApp(System.Collections.Generic.List<string> applist)
        {
            GetOtherAppCommand command = new GetOtherAppCommand(L2C.UserID, L2C.Key);
            command.set(Convert.ToInt64( appinfo.appID), applist);
            command.execute(L2C.UpgradeURL);
            return true;
        }

        public bool GetChatList()
        {
            GetChatListCommand command = new GetChatListCommand(L2C.UserID.ToString(), System.Text.Encoding.UTF8.GetString(L2C.Key, 0, L2C.Key.Length));
            command.execute(L2C.UpgradeURL);
            return true;
        }

        //public bool ModifyUser()
        //{
        //    ModifyUserCommand command = new ModifyUserCommand(L2C.UserID.ToString(), System.Text.Encoding.UTF8.GetString(L2C.Key, 0, L2C.Key.Length), user);

        //    //command.execute(L2C.UpgradeURL);
        //    return true;
        //}
#endregion
    }
    #region TYPE
    //Enum
    public enum friendCtrlTypeEnum { notype, search, friend, stranger, };
    public enum chatroomCtrlTypeEnum { notype, create, chatroom, };
    public enum msgTypeEnum { notype, receiveTxt, receiveVoice, receivePic, sendTxt, sendVoice, sendPic, };
    #endregion
    #region AppInfo
    public class AppInfo
    {
        private static AppInfo instance;
        private static AppInfo Instance
        {
            get { return instance; }
            set { if (instance == null) { instance = new AppInfo(); } }
        }
        private static AppInfo getInstance()
        {
            if (instance == null)
            {
                instance = new AppInfo();
            }
            return instance;
        }
        public string version { get { return "1000"; } }
        public string ipAddr { get { return "testlg.gotye.com.cn"; } }
        public string deviceID { get { return "0b65caa0-18ab-4"; } }
        //String appID = "0b65caa0-18ab-4c6b-aeab-5a55d9f62b6c";
        public string appID { get { return "123"; } }
        public string appVer { get { return "1.0"; } }
    }
#endregion
#region Login Info
    public class l2cInfo
    {
        public int Result { get; set; }
        public string Ip { get; set; }
        public int Port { get; set; }
        public long UserID { get; set; }
        public byte[] Key { get; set; }
        public string UpgradeURL { get; set; }
        //public int result { get; set; }
        //public int result { get; set; }
        public l2cInfo(byte[] buffer, int bytesTransferred)
        {
            parse(buffer, bytesTransferred);
        }
        private void parse(byte[] buffer, int bytesTransferred)
        {
            Qplus.protocol.L2C_RESP_LOGIN l2c = new Qplus.protocol.L2C_RESP_LOGIN();
            l2c.setData(buffer);
            l2c.handle(buffer, 0, bytesTransferred);
            Result = l2c.getResult();
            Ip = l2c.getIP();
            Port = l2c.getPort();
            UserID = l2c.getUserID();
            Key = l2c.getKey();
            UpgradeURL = l2c.getUpgradeURL();
        }
    }
    public class u2cRESPmsg
    {
        public long AppID { get; set; }
        public int UserLevel { get; set; }
        public int Result { get; set; }

        public u2cRESPmsg(byte[] buffer, int bytesTransferred)
        {
            parse(buffer, bytesTransferred);
        }
        private void parse(byte[] buffer, int bytesTransferred)
        {
            Qplus.protocol.U2C_RESP_LOGIN u2c = new Qplus.protocol.U2C_RESP_LOGIN();
            u2c.setData(buffer);
            u2c.handle(buffer, 0, bytesTransferred);
            Result = u2c.getResult();
            AppID = u2c.getAppID();
            UserLevel = u2c.getUserLevel();
        }
    }
    public class HttpData
    {
        public user[] userList { get; set; }
        public room[] roomList { get; set; }
        public chatroom chatroom { get; set; }
        //public userInfo user { get; set; }
    }
#endregion
#region userList
    public class userList
    {
        private static userList instance;

        public static userList getInstance()
        {
            if (instance == null)
            {
                instance = new userList();
            }
            return instance;
        }
        public userList update(JObject jo)
        {
            try
            {
                var userNode = jo["userList"].First;
                LinkedList<user> ul = new LinkedList<user>();
                //user[] ul = new user[jo.Count];
                while (null != userNode )
                {
                    ul.AddLast(new user()
                    {
                        userID = (int)userNode["userID"],
                        userName = (string)userNode["userName"],
                        userLevel = (string)userNode["userLevel"],
                        headType = (int)userNode["headType"],
                        headValue = (string)userNode["headValue"]
                    });
                    userNode = userNode.Next;
                }
                list = ul;
                return this;
            }
            catch (Exception ee)
            {
                Debug.WriteLine(ee.Message);
                return null;
            }
        }

        public LinkedList<user> list { get; set; }
    }
    //[DataContract(Namespace = "http://testap.gotye.com.cn:8001/AppPortlet/")]
    public class user
    {
        //[DataMember(Order = 0)]
        public int userID { get; set; }
        //[DataMember(Order = 1)]
        public string userName { get; set; }
        //[DataMember(Order = 2)]
        public string userLevel { get; set; }
        //[DataMember(Order = 3)]
        public int headType { get; set; }
        //[DataMember(Order = 4)]
        public string headValue { get; set; }
    }
#endregion
    #region chatRoom
    public class roomList
    {
        private static roomList instance;

        public static roomList getInstance()
        {
            if (instance == null)
            {
                instance = new roomList();
            }
            return instance;
        }
        public roomList update(JObject jo)
        {
            try
            {
                var roomNode = jo["roomList"].First;
                LinkedList<room> rl = new LinkedList<room>();
                while (roomNode != null)
                {
                    rl.AddLast(new room()
                    {
                        roomID = (int)roomNode["roomID"],
                        roomName = (string)roomNode["roomName"],
                        roomLevel = (int)roomNode["roomLevel"],
                        userCount = (int)roomNode["userCount"],
                        upperLimit = (int)roomNode["upperLimit"],
                        roomHeadType = (int)roomNode["roomHeadType"],
                        roomHeadValue = (string)roomNode["roomHeadValue"],
                        roomHeat = (int)roomNode["roomHeat"],
                        roomPoint = (int)roomNode["roomPoint"],
                    });
                    roomNode = roomNode.Next;
                }
                list = rl;
                return this;
            }
            catch (Exception ee)
            {
                Debug.WriteLine(ee.Message);
                return null;
            }
        }

        public LinkedList<room> list { get; set; }
    }
    public struct chatroom
    {
        public int roomID { get; set; }
        public string roomName { get; set; }
        public int roomLevel { get; set; }
        public int userCount { get; set; }
        public int upperLimit { get; set; }
        public int roomHeadType { get; set; }
        public string roomHeadValue { get; set; }
        //public int roomHeat { get; set; }
        //public int roomPoint { get; set; }
    }
    public class room
    {
        public int roomID { get; set; }
        public string roomName { get; set; }
        public int roomLevel { get; set; }
        public int userCount { get; set; }
        public int upperLimit { get; set; }
        public int roomHeadType { get; set; }
        public string roomHeadValue { get; set; }
        public int roomHeat { get; set; }
        public int roomPoint { get; set; }
        //public int roomHeadType { get; set; }
    }
    public class roomCtrl
    {
        public chatroomCtrlTypeEnum type { get; set; }
        public int roomID { get; set; }
        public string roomName { get; set; }
        public int roomLevel { get; set; }
        public int userCount { get; set; }
        public int upperLimit { get; set; }
        public int roomHeadType { get; set; }
        public string roomHeadValue { get; set; }
        public int roomHeat { get; set; }
        public int roomPoint { get; set; }
    }
    public class chatList
    {
        private static chatList instance;

        public static chatList getInstance()
        {
            if (instance == null)
            {
                instance = new chatList();
            }
            return instance;
        }
        public int peerType { get; set; }
        public int peerID { get; set; }
        public string peerName { get; set; }
        public int peerLevel { get; set; }
        public int headType { get; set; }
        public string headValue { get; set; }
    }
#endregion
#region OTHER
    public class AppOption
    {
        public string ctrlOption { get; set; }
    }
    public class Res
    {
        //file
    }
    public class friend
    {
        public friendCtrlTypeEnum type { get; set; }
        public int userID { get; set; }
        public string userName { get; set; }
        public string userLevel { get; set; }
        public int headType { get; set; }
        public string headValue { get; set; }
    }
#endregion
}
